package display3d.away3d {
	import away3d.containers.ObjectContainer3D;
	import away3d.materials.ColorMaterial;
	import away3d.primitives.GeodesicSphere;

	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;

	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;
	
	
	public class Spark3D extends ObjectContainer3D
	{
		private var centerParticle:GeodesicSphere;
		
		private var rotationTimer:Timer;
		private var particleTimer:Timer;
		
		private var particles:Vector.<GeodesicSphere>;
		
		public var sparkScale:Number = 0;
		
		
		public function Spark3D()
		{
			super();
			
			ownCanvas = true;
			filters = [new GlowFilter(0x3377FF, 1 , 20, 20, 2, 1, false)];
			
			this.blendMode = BlendMode.ADD;
			
			material = new ColorMaterial(0xFFFFFF);
			
			centerParticle = new GeodesicSphere();
			centerParticle.radius = 100;
			centerParticle.fractures = 1;
			centerParticle.material = material;
		
			addChild(centerParticle);
			centerParticle.scaleX = centerParticle.scaleY = centerParticle.scaleZ = 0.01;
					
			rotationTimer = new Timer(30, 0);
			rotationTimer.addEventListener(TimerEvent.TIMER, rotateParticle);
			rotationTimer.addEventListener(TimerEvent.TIMER, resize);
			rotationTimer.start();
			
			particleTimer = new Timer(50, 0);
			particleTimer.start();
			particleTimer.addEventListener(TimerEvent.TIMER, addParticle);
			
			particles = new Vector.<GeodesicSphere>();
		}
		
		private function getNewParticle():GeodesicSphere{
			var particle:GeodesicSphere = new GeodesicSphere();
			particle.radius = 50;
			particle.fractures = 1;
			particle.material = material;
			
			return particle;
		}
		
		private function addParticle(evt:Event):void{
			
			if(sparkScale == 0) return;
			
			var angleX:Number = Math.random()*(Math.PI*2);
			var angleY:Number = Math.random()*(Math.PI*2);
			var tempParticle:GeodesicSphere = particles.length > 0 ? particles.shift() : getNewParticle();
			var distance:Number = centerParticle.radius*centerParticle.scaleX/2 + 150;
			tempParticle.x = Math.sin(angleY)*distance + Math.cos(angleY)*distance;
			tempParticle.y = Math.cos(angleX)*distance + Math.sin(angleX)*distance;
			addChild(tempParticle);
			tempParticle.scaleX = tempParticle.scaleY = tempParticle.scaleZ = .1;
			
			TweenMax.to(tempParticle, .5, {x:0, y:0, z:0, scaleX:centerParticle.scaleX, scaleY:centerParticle.scaleY, scaleZ:centerParticle.scaleZ, ease:Quad.easeOut, onComplete:removeParticle, onCompleteParams:[tempParticle]});
		}

		private function removeParticle(p_particle:GeodesicSphere):void{
			removeChild(p_particle);
			particles.push(p_particle);
		}

		private function resize(evt:Event):void{
			var _scale:Number = centerParticle.scaleX; 
			if(centerParticle.scaleX > sparkScale) _scale -= .01;
			if(centerParticle.scaleX < sparkScale) _scale += .01;
			if(_scale==0) {
				_scale =.01;
				centerParticle.visible=false;
			}else{
				centerParticle.visible=true;
			}
			centerParticle.scale(_scale); 
		}
		
		private function rotateParticle(evt:Event):void{
			centerParticle.rotationY+=5;
			centerParticle.rotationX+=2;
			centerParticle.rotationZ+=1;
		}
	}
}